package com.arithmeticEveryDay.blueBridge;

import java.math.BigInteger;
 
public class LuckyNumber2 {
    public static void main(String[] args) {
        int i = 1, count = 0;//count记录次数
        while (true) {
            String bin = Integer.toBinaryString(i);//二进制
            String Oct = Integer.toOctalString(i);//八进制
            String ten = String.valueOf(i);//十进制转为字符串
            String hex = Integer.toHexString(i);//十六进制
 
            int binSum=sum(bin,2);
            int octSum=sum(Oct,8);
            int tenSum=sum(ten,10);
            int hexSum=sum(hex,16);
 
            //如果十进制数被它的二进制，八进制，十进制，十六进制的各数之和整除，就是幸运数字
            if(i%binSum==0 && i%octSum==0 && i%tenSum==0 && i%hexSum==0){
                //System.out.println(i);
                count++;
            }
            if(count==2023){//到第2023个幸运数字
                System.out.println(i);
                break;
            }
            i++;
        }
    }
 
    public static int sum(String numStr, int radix) {
        BigInteger b1 = new BigInteger("0", radix);
        for (int i = 0; i < numStr.length(); i++) {
            BigInteger b2 = new BigInteger(numStr.substring(i, i + 1), radix);
            b1 = b1.add(b2);
        }
        return b1.intValue();
    }
}